<?php
include dirname(dirname(dirname(__FILE__))).'/include/user-verify.php';
include dirname(dirname(__FILE__)).'/user-manage/functions.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';
include dirname(__FILE__).'/functions.php';

$cur_id = $_SESSION['user_id'];
$cur_user = getUserById($cur_id);
if($cur_user['exchange_status']=='2'||$cur_user['exchange_status']=='1') {
	echo '<script type="text/javascript">alert("有一个关于你的换班请求尚未被处理，请待其处理完毕后再提交请求！");
		 window.location="../admin-home.php";</script>';
	exit();
}
$duty_table = getCurDutyTable();
$user_map = getCurDutyUser();
$user_map[""]='';
$option_table = array();
for($i=1;$i<=7;$i++) {
	foreach(range('a', 'f') as $item) {
		if($duty_table[$i][$item]!=$cur_id) {
			$option_table[$i][$item]='<option value="'.$duty_table[$i][$item].'">'.$user_map[$duty_table[$i][$item]].'</option>';
			$option_table[$i][$item].='<option value="'.$cur_id.'">自己</option>';
		} else {
			$option_table[$i][$item]='<option value="'.$cur_id.'">自己</option>';
			$option_table[$i][$item].='<option value="" class="to-change"></option>';
		}
		
	}
}


?>


<!DOCTYPE html>
<html>
	<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php'; ?>
		<link rel="stylesheet" type="text/css" href="../css/style.css">
		<script type="text/javascript">
			$(document).ready(function() {
				
				$("#to-exchange-user").change(function() {
					for(var i=1;i<=7;i++) {
						for(var item='a';item<='f';item++) {
							$("#"+i+"_"+item)[0].options[0].selected=true;
						}
					}
					var to_exchang_user_id = $(this).children('option:selected').val();
					var to_exchang_user_name = $(this).children('option:selected').text();
					var this_id = <?php echo $cur_id; ?>;
					if(to_exchang_user_id=='') {
						$("select").attr("disabled","disabled");
						$(this).removeAttr("disabled");
						$("#exchange-submit-btn").attr("disabled","disabled");
						return;
					}
					$("select").attr("disabled","disabled");
					$("select[mark='"+to_exchang_user_id+"']").removeAttr("disabled");
					$("select[mark='"+this_id+"']").removeAttr("disabled");
					$("select option[class='to-change']").val(to_exchang_user_id);
					$("select option[class='to-change']").text(to_exchang_user_name);
					/*var weekday = <?php echo date('w'); ?>;
					if(weekday==0)weekday=7;
					for(var i=1;i<=weekday;i++) {
						$("select[id^='"+i+"']").attr("disabled","disabled");
					}*/
					$(this).removeAttr("disabled");
				});
				$(".normal-select").change(function() {
					var select_set = $(".normal-select");
					var flag = true;
					for(var i=0;i<select_set.length;i++) {
						if(select_set[i].selectedIndex!="0") {
							$("#exchange-submit-btn").removeAttr("disabled");
							$("#remark").removeAttr("disabled");
							flag=false;
							break;
						}
					}
					if(flag) {
						$("#exchange-submit-btn").attr("disabled","disabled");
						$("#remark").attr("disabled","disabled");
					}
				});
				$("#exchange-submit-btn").click(function() {
					if($("#to-exchange-user").children('option:selected').val()=='') {
						alert("请选择换班对象！");
						$(this).attr("disabled","disabled");
						return false;
					}
					$("select").removeAttr("disabled");
					return true;
				});
			});
		</script>
	</head>
	<body>
	<div class="col-md-12">
		<div>
			<h3> 请求换班</h3>
		</div>
		<div>
			<form class="form-horizontal" role="form" method="post" action="exchange-duty-submit.php">
				<div class="form-group">
				    <label for="to-exchange-user" class="col-sm-2 control-label" style="text-align:left;width:13%">请求换班对象</label>
				    <div class="col-sm-10">
				      <select class="form-control" name="to-exchange-user" id="to-exchange-user" style="width:100px;">
						<option value=""></option>
						<?php 
							foreach ($user_map as $key => $value) {
								if($key!=$cur_id&&$key!='') {
									echo '<option value="'.$key.'">'.$value.'</option>';
								}
							}
						?>
					</select>
				    </div>
				</div>


				<span>注：一次只能支持与一个人换班，换班仅本周有效</span>
				<table class="table table-striped table-bordered table-hover table-condensed" id="default-table">
				  <tr>
				  	<th>#</th>
				  	<th>星期一</th>
				  	<th>星期二</th>
				  	<th>星期三</th>
				  	<th>星期四</th>
				  	<th>星期五</th>
				  	<th>星期六</th>
				  	<th>星期天</th>
				  </tr>
				  <?php 
				  $time = array();
				  $time['a']='7:30~10:00';$time['b']='10:00~12:30';
				  $time['c']='12:30~14:30';$time['d']='14:30~16:00';
				  $time['e']='16:00~18:30';$time['f']='18:30~22:30';
				  foreach(range('a', 'f') as $item) {
				  	echo '<tr>';
				  	echo '<td style="vertical-align:middle;">'.$time[$item].'</td>';
				  	for($i=1;$i<=7;$i++) {
				  		echo '<td>';
				  		echo '<select  disabled class="form-control normal-select" id="'.$i.'_'.$item.'" name="'.$i.'_'.$item.'" mark="'.$duty_table[$i][$item].'">';
				  		echo $option_table[$i][$item];
				  		echo '</select>';
				  		echo '</td>';
				  	}
				  	echo '<tr>';
				  }
				  ?>
				</table>

				<div class="form-group">
				    <label for="remark" class="col-sm-2 control-label" style="text-align:left;width:11%">备注信息</label>
				    <div class="col-sm-10">
				      <textarea class="form-control" rows="8" id="remark" name="remark" placeholder="备注信息" style="width:80%" disabled="disabled"></textarea>
				    </div>
				</div>



				<button type="submit" class="btn btn-primary" id="exchange-submit-btn" disabled="disabled">提交换班请求</button>
			</form>
		</div>
	</div>
	</body>
</html>
